package juc.readwrite;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWrite {
    public static void main(String[] args) {
        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        lock.readLock().lock();
        System.out.println("主线程获取到读锁");
        new Thread(()->{
            lock.writeLock().lock();
            System.out.println(Thread.currentThread().getName()+" : 获取到锁");
            lock.writeLock().unlock();
            System.out.println(Thread.currentThread().getName()+" : 释放锁");
        },"子线程").start();
        System.out.println("主线程获取到读锁-子线程阻塞");
        lock.readLock().unlock();
        System.out.println("主线程释放到读锁-子线获取写锁中。。。。");
    }
}
